/* !点击logo回到首页 */
$('.logoimg').on('click', function () {
    location.href='../html/huaindex.html'
})
/* !手机邮箱切换 */
$('.regist_type p').on('click', function () {
    $(this).addClass('active').siblings().removeClass('active');
    $('.regist_form form').eq($(this).index()).css({
        display: 'block'
    }).siblings().css({
        display: 'none'
    })
});

/* !表单验证 */
/* !验证手机号 */
$.validator.addMethod('teltest', function (value) {
    let reg = /^1[3-9]\d{9}$/;
    if (reg.test(value)) {
        return true;
    }
    return false;
}, '请输入正确手机号码');
/* !验证邮箱 */
 $.validator.addMethod('mailtest', function (value) {
    let reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
    if (reg.test(value)) {
        return true;
    }
    return false;
}, '请输入正确的邮箱格式');
/* !验证码 */
function createCode(code) {
            let c = '';
            for (let i = 1; i <= code; i++) {
                c += parseInt(Math.random() * 10);
            }
            return c;
        }
$('.yzm').on('click', function () {
            $('.yzm').html(createCode(6));
        })
$.validator.addMethod('msgtest', function (value) {
    if (value == $('.yzm').html()) {
        return true;
    }
    return false;
}, '验证码错误');

/* !验证密码 8-14个字符 数字 字母 标点标点符号 组成 至少有两种符号组成 */
$.validator.addMethod("pawdtest", function (value) {
    let reg = /^(?![a-z]+$)(?![\d]+$)(?![!@#$%^&*<?\.]+$)[\da-z\.*&,?@#$%^-]{6,10}$/i;
    if (reg.test(value)) {
        return true;
    }
    return false;
}, '6-10个字符组成，不能全为数字字符符号')

let phonenum = document.querySelector('#phonenum');
let password = document.querySelector('#password');
$('#form1').validate({
    rules: {
        tel: {
            required: true,
            teltest: true,            
        },
        pawd: {
            required: true,
            pawdtest: true,
        },
        msg: {
            required:true,
            msgtest:true,
        }
    },
    messages: {
        tel: {
            required: '请输入您的邮箱账号或手机号码',
        },
        pawd: {
            required: '请输入正确的密码',
        },
        msg: {
            required: '请输入验证码',
        }
    },
    submitHandler: function (form) {
        let p = pAjax({
            url: '/flo/regist.php',
            type: 'post',
            data: {
                phonenum: phonenum.value,
                password: password.value,
            },
        });
        p.then((res) => {
            res = JSON.parse(res);
            if (res.code) {
                alert('注册成功，请登录');
                // setCookie('login', phonenum.value);
                // let url = localStorage.getItem('url');
                // localStorage.removeItem('url');
                // location.href = url ? url : '../html/huaindex.html';
                location.href = '../html/login.html';
            }          
        })
    },
});
let comreg = document.querySelector('.comreg');
let compawd = document.querySelector('.compawd');
$('#form2').validate({
    rules: {
        mail: {
            required: true,
            mailtest: true,            
        },
        pawd: {
            required: true,
            pawdtest: true,
        },
    },
    messages: {
        mail: {
            required: '请输入您的邮箱账号',
        },
        pawd: {
            required: '请输入正确的密码',
        }
    },
    submitHandler: function (form) {
        let p = pAjax({
            url: '/flo/regist.php',
            type: 'post',
            data: {
                phonenum: comreg.value,
                password: compawd.value,
            },
        });
        p.then((res) => {
            res = JSON.parse(res);
            if (res.code) {
                 alert('注册成功，请登录');
                location.href = '../html/login.html';
            }          
        })
    },
});
